<?php

//decode by http://www.yunlu99.com/
require MODULE_ROOT . '/helpers/functions.php';
global $_GPC, $_W;
$op = $_GPC['op'] ?: 'index';
$user = user();
$config = config();
$group = $user['group'] ? get_group_info_by_id($user['group']) : false;
$uniacid = uniacid();
check_auth();
if ($op == 'index') {
	$userInfo = pdo_get('d1sj_card_user', ['uid' => uid(), 'uniacid' => $uniacid]);
	$verify = $userInfo['name'] && $userInfo['certify'] && $userInfo['mobile'];
	include $this->template('user');
} else {
	if ($op == 'profile') {
		if (is_post()) {
			$data = input('data');
			if (!preg_match("/^[\x{4e00}-\x{9fa5}]+$/u", $data['name'])) {
				message('请填写正确的用户名');
			}
			if (!$this->is_idcard($data['certify'])) {
				message('身份证格式错误');
			}
			if (!preg_match("/^1[34578]\d{9}$/", $data['mobile'])) {
				message('手机号格式不正确');
			}
			$data['openid'] = $_W['openid'];
			pdo_update('d1sj_card_user', $data, ['uid' => uid()]);
			message('保存成功', $this->createMobileUrl('vip'), 'success');
		}
		include $this->template('user/profile');
	} else {
		if ($op == 'record') {
			$uniacid = uniacid();
			$uid = uid();
			$where = ["uniacid = {$uniacid}", "uid = {$uid}"];
			$sql = 'SELECT * FROM ' . tablename('d1sj_card_record') . ' WHERE ' . implode(' AND ', $where) . ' order by id desc';
			$logs = pdo_fetchall($sql);
			$bankSql = 'SELECT * FROM ' . tablename('d1sj_card_card') . 'WHERE uniacid = :uniacid';
			$banks = pdo_fetchall($bankSql, [':uniacid' => $uniacid]);
			$lendSql = 'SELECT * FROM ' . tablename('d1sj_card_lend') . 'WHERE uniacid = :uniacid';
			$lends = pdo_fetchall($lendSql, [':uniacid' => $uniacid]);
			$tmp = [];
			foreach ($banks as $bank) {
				$tmp[$bank['id']] = $bank;
			}
			$banks = $tmp;
			$tmp = [];
			foreach ($lends as $lend) {
				$tmp[$lend['id']] = $lend;
			}
			$lends = $tmp;
			$statusText = [0 => '待审核', 1 => '已成功', 2 => '已拒绝'];
			include $this->template('user/record');
		} else {
			if ($op == 'logs') {
				$level = input('level', 0);
				$filter = ['level' => $level, 'op' => $op];
				$uniacid = uniacid();
				$uid = uid();
				if (is_post()) {
					$where = ["uniacid = {$uniacid}", "uid = {$uid}"];
					if ($tmpInput = input('level')) {
						array_push($where, "level = {$tmpInput}");
					}
					$count = 'SELECT count(id) FROM ' . tablename('d1sj_card_logs') . ' WHERE ' . implode(' AND ', $where);
					$total = get_page_total($count);
					$sql = 'SELECT level,createtime,money FROM ' . tablename('d1sj_card_logs') . ' WHERE ' . implode(' AND ', $where) . ' ' . get_limit();
					$lists = pdo_fetchall($sql);
					foreach ($lists as $key => $list) {
						$lists[$key]['createtime'] = date('Y-m-d H:i:s', $list['createtime']);
					}
					return_json(['result' => $lists, 'total' => $total, 'code' => count($lists) ? 1 : 0, 'reason' => count($lists) ? '' : '没有更多了']);
				}
				include $this->template('user/logs');
			} else {
				if ($op == 'team') {
					$level = input('level', 0);
					$filter = ['level' => $level, 'op' => $op];
					$uid = uid();
					if (is_post()) {
						$where = ["u.uniacid = {$uniacid}", 'u.leader' . ($level + 1) . " = {$uid}"];
						$count = 'SELECT count(u.id) FROM ' . tablename('d1sj_card_user') . ' as u WHERE ' . implode(' AND ', $where);
						$total = get_page_total($count);
						$sql = 'SELECT m.nickname,g.name `group`,u.`uid` FROM ' . tablename('d1sj_card_user') . ' as u LEFT JOIN ' . tablename('mc_members') . ' as m on m.uid = u.uid LEFT JOIN ' . tablename('d1sj_card_group') . ' as g on u.group = g.id WHERE ' . implode(' AND ', $where) . ' ' . get_limit();
						$lists = pdo_fetchall($sql);
						foreach ($lists as $key => $list) {
							$info = mc_fansinfo($list['uid']);
							$lists[$key]['avatar'] = $info['avatar'];
							$lists[$key]['level'] = $level + 1;
							$lists[$key]['group'] = $list['group'] ?: '';
						}
						return_json(['result' => $lists, 'total' => $total, 'code' => count($lists) ? 1 : 0, 'reason' => count($lists) ? '' : '没有更多了']);
					}
					include $this->template('user/team');
				} else {
					if ($op == 'fan') {
						$level = input('level', 0);
						$filter = ['level' => $level, 'op' => $op];
						$uniacid = uniacid();
						$uid = uid();
						if (is_post()) {
							$where = ["uniacid = {$uniacid}"];
							if ($tmpInput = $level) {
								array_push($where, "fan_uid{$level} = {$uid}");
							} else {
								array_push($where, "(fan_uid1 = {$uid} OR fan_uid2 = {$uid} OR fan_uid3 = {$uid})");
							}
							$count = 'SELECT count(id) FROM ' . tablename('d1sj_card_record') . ' WHERE ' . implode(' AND ', $where);
							$total = get_page_total($count);
							$sql = 'SELECT uid,fan_uid1,fan_uid2,fan_uid3,fan_level1,fan_level2,fan_level3,createtime,type,bank,status FROM ' . tablename('d1sj_card_record') . ' WHERE ' . implode(' AND ', $where) . ' ' . get_limit();
							$lists = pdo_fetchall($sql);
							$tmp = [];
							$status = [0 => '未审核', 1 => '审核通过', 2 => '审核失败'];
							foreach ($lists as $key => $list) {
								$tmp[$key]['createtime'] = date('Y-m-d H:i:s', $list['createtime']);
								if ($list['type'] == 1) {
									$bank_list_name = pdo_fetch('select name from ' . tablename('d1sj_card_bank') . ' where id = :id', [':id' => $list['bank']]);
									$tmp[$key]['type'] = '办理' . $bank_list_name['name'] . '业务';
								} elseif ($list['type'] == 2) {
									$lend_list_name = pdo_fetch('select title from ' . tablename('d1sj_card_lend') . ' where id = :id', [':id' => $list['bank']]);
									$tmp[$key]['type'] = '办理' . $lend_list_name['title'] . '业务';
								} elseif ($list['type'] == 3) {
									$group_list_name = pdo_fetch('select name from ' . tablename('d1sj_card_group') . ' where id = :id', [':id' => $list['bank']]);
									$tmp[$key]['type'] = '用户升级' . $group_list_name['name'] . "业务";
								}
								if ($list['type'] == 3) {
									$tmp[$key]['status'] = '升级成功';
								} else {
									$tmp[$key]['status'] = $status[$list['status']];
								}
								$info = mc_fansinfo($list['uid']);
								$tmp[$key]['name'] = $info['nickname'];
								$tmp[$key]['avatar'] = $info['avatar'];
								if ($level) {
									$tmp[$key]['money'] = $list['fan_level' . $level];
									$tmp[$key]['level'] = $level;
								} else {
									if ($list['fan_uid1'] == $uid) {
										$tmp[$key]['money'] = $list['fan_level1'];
										$tmp[$key]['level'] = 1;
									} else {
										if ($list['fan_uid2'] == $uid) {
											$tmp[$key]['money'] = $list['fan_level2'];
											$tmp[$key]['level'] = 2;
										} else {
											if ($list['fan_uid3'] == $uid) {
												$tmp[$key]['money'] = $list['fan_level3'];
												$tmp[$key]['level'] = 3;
											}
										}
									}
								}
							}
							$lists = $tmp;
							return_json(['result' => $lists, 'total' => $total, 'code' => count($lists) ? 1 : 0, 'reason' => count($lists) ? '' : '没有更多了']);
						}
						include $this->template('user/fan');
					} else {
						if ($op == 'cash') {
							$status = input('status', 1);
							$filter = ['status' => $status, 'op' => $op];
							$uniacid = uniacid();
							$uid = uid();
							if (is_post()) {
								$where = ["uniacid = {$uniacid}", "uid = {$uid}"];
								if ($tmpInput = input('status')) {
									array_push($where, "status = {$tmpInput}");
								}
								$count = 'SELECT count(id) FROM ' . tablename('d1sj_card_cash') . ' WHERE ' . implode(' AND ', $where);
								$total = get_page_total($count);
								$sql = 'SELECT status,createtime,money FROM ' . tablename('d1sj_card_cash') . ' WHERE ' . implode(' AND ', $where) . ' ' . get_limit();
								$lists = pdo_fetchall($sql);
								$statusText = [1 => '未审核', 2 => '已提现', 3 => '已拒绝'];
								foreach ($lists as $key => $list) {
									$lists[$key]['createtime'] = date('Y-m-d H:i:s', $list['createtime']);
									$lists[$key]['status'] = $statusText[$list['status']];
								}
								return_json(['result' => $lists, 'total' => $total, 'code' => count($lists) ? 1 : 0, 'reason' => count($lists) ? '' : '没有更多了']);
							}
							include $this->template('user/cash');
						} else {
							if ($op == 'tocash') {
								if (is_post()) {
									$money = input('data')['money'];
									$alipay = input('data')['alipay'];
									if (!$money) {
										message('请输入提现的金额');
									}
									if ($user['money'] < $money) {
										message('余额不足');
									}
									if ($money < $config['cash']) {
										message('未满足提现要求');
									}
									if ($user['alipay'] == '') {
										if (!$alipay) {
											message('请填写支付宝账号');
										} else {
											$up_user_date = array('alipay' => $alipay);
											$user_result = pdo_update('d1sj_card_user', $up_user_date, array('id' => $user['id']));
											if (!$user_result) {
												message('操作失败，请重试');
											}
										}
									}
									if ($user['name'] == '' || $user['mobile'] == '' || $user['certify'] == '') {
										message('请完善个人资料后再试', $this->createMobileUrl('user', ['op' => 'profile']), 'error');
									}
									$data = ['uniacid' => uniacid(), 'uid' => uid(), 'money' => $money, 'status' => 1, 'alipay' => $alipay ? $alipay : $user['alipay'], 'mobile' => $user['mobile'], 'name' => $user['name'], 'createtime' => time()];
									$cash_result = pdo_insert('d1sj_card_cash', $data);
									if ($cash_result) {
										message('申请成功', $this->createMobileUrl('user', ['op' => 'cash']));
									} else {
										message('操作失败，请重试');
									}
								}
								include $this->template('user/tocash');
							} else {
								if ($op == 'qrcode') {
									$uid = uid();
									$moduleName = $this->modulename;
									$path = MODULE_ROOT . "/attach/card/{$uniacid}_{$_W['acid']}_{$user['uid']}.png";
									if (!file_exists($path)) {
										$qrimg = $config['bg_qrcode'] ? imagecreatefromstring(file_get_contents($_W['attachurl'] . $config['bg_qrcode'])) : imagecreatefrompng(MODULE_ROOT . '/static/images/qrcode.png');
										$bgData = explode('|', $config['bg_data']);
										$bgAvatar = explode('|', $config['bg_avatar']);
										$avatar = imagecreatefromstring(file_get_contents($_W['fans']['avatar']));
										imagecopy($qrimg, $avatar, $bgAvatar[0] ?: 100, $bgAvatar[1] ?: 200, 0, 0, imagesx($avatar), imagesy($avatar));
										$this->createMobileUrl();
										$url = 'http://qr.liantu.com/api.php?&w=' . ($bgData[3] ?: 200) . '&text=' . urlencode(mobile_url($this->modulename, 'user', ['cc' => $uid]));
										$qr = imagecreatefromstring(file_get_contents($url));
										imagecopy($qrimg, $qr, $bgData[0] ?: 300, $bgData[1] ?: 500, 0, 0, $bgData[2] ?: 200, $bgData[3] ?: 200);
										imagepng($qrimg, $path);
									}
									include $this->template('user/qrcode');
								} else {
									if ($op == 'invite') {
										$setting = $config;
										include $this->template('user/invite');
									} else {
										if ($op == 'income') {
											$type = input('type', 2);
											$filter = ['type' => $type, 'op' => $op];
											$uniacid = uniacid();
											$uid = uid();
											if (is_post()) {
												$where = ["uniacid = {$uniacid}", "fan_uid1 = {$uid}"];
												if ($tmpInput = input('type')) {
													array_push($where, "type = {$tmpInput} and status =1");
												}
												$count = 'SELECT count(id) FROM ' . tablename('d1sj_card_record') . ' WHERE ' . implode(' AND ', $where);
												$total = get_page_total($count);
												$sql = 'SELECT createtime,fan_level1 FROM ' . tablename('d1sj_card_record') . ' WHERE ' . implode(' AND ', $where) . ' ' . get_limit();
												$lists = pdo_fetchall($sql);
												foreach ($lists as $key => $list) {
													$lists[$key]['createtime'] = date('Y-m-d H:i:s', $list['createtime']);
													$lists[$key]['fan_level1'] = $list['fan_level1'];
												}
												return_json(['result' => $lists, 'total' => $total, 'code' => count($lists) ? 1 : 0, 'reason' => count($lists) ? '' : '没有更多了']);
											}
											include $this->template('user/income');
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}